package logic

import (
	"context"
	"errors"
	"gozero-microservices/common-service/rpc/types/common"
	"gozero-microservices/gateway-api/internal/svc"
	"gozero-microservices/gateway-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

/*
 +----------------------------------------------------------------------
 + Title        : RegionLogic
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2025-08-08
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 区域逻辑
 +----------------------------------------------------------------------
*/

type RegionLogic struct {
	BaseLogic
}

/**
 * 创建区域逻辑实例
 *
 * @param context.Context ctx ----------------------必传
 * @param *svc.ServiceContext svcCtx ---------------必传
 * @return *RegionLogic
 * @author huwl
 * @date 2025/10/22 12:54:03
 */
func NewRegionLogicInstance(ctx context.Context, svcCtx *svc.ServiceContext) *RegionLogic {
	return &RegionLogic{
		BaseLogic{
			Ctx:    ctx,
			SvcCtx: svcCtx,
			Logger: logx.WithContext(ctx),
		},
	}
}

/**
 * 详情
 *
 * @param uint16 Id ---------------------------必传,Id
 * @return *common.Region, error
 * @author huwl
 * @date 2025/10/22 10:15:55
 */
func (l *RegionLogic) Detail(id uint16) (*common.Region, error) {
	if id <= 0 {
		return nil, errors.New("Id错误")
	}

	// 远程调用公共服务之区域服务
	return l.SvcCtx.RegionSrv.RegionDetail(l.Ctx, &common.DetailRequest{Id: uint64(id)})
}

/**
 * 列表
 *
 * @param uint16 Id ---------------------------非必传,Id
 * @param string Ids --------------------------非必传,Id支持多个
 * @param string Pid --------------------------非必传,父级Id
 * @param string Pids -------------------------非必传,父级Id支持多个
 * @param string Name -------------------------非必传,名称
 * @param uint8 Level -------------------------非必传,级别
 * @param string IsHot ------------------------非必传,是否热门
 * @param string SortField --------------------非必传,排序字段
 * @param string SortType ---------------------非必传,排序方式
 * @param int8 PageSize -----------------------非必传,条数
 * @param uint8 Page --------------------------非必传,页码
 * @return *common.RegionListResponse, error
 * @author huwl
 * @date 2025/10/22 10:15:55
 */
func (l *RegionLogic) List(params types.RegionQueryRequest) (*common.RegionListResponse, error) {
	pid := params.Pid
	isHot := params.IsHot

	if pid == "" {
		pid = "all"
	}
	if isHot == "" {
		pid = "all"
	}

	// 远程调用公共服务之区域服务
	return l.SvcCtx.RegionSrv.RegionList(l.Ctx, &common.QueryRegionRequest{
		Id:        uint32(params.Id),
		Ids:       params.Ids,
		Pid:       pid,
		Pids:      params.Pids,
		Name:      params.Name,
		Level:     uint32(params.Level),
		IsHot:     isHot,
		Page:      uint32(params.Page),
		PageSize:  int32(params.PageSize),
		SortField: params.SortField,
		SortType:  params.SortType,
	})
}
